# -*- encoding=utf8 -*-

__author__ = "code哥"
import random
import sys
from airtest.core.api import *
from poco.drivers.android.uiautomation import AndroidUiautomationPoco

dev = False

if dev:
    poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
    from airtest.cli.parser import cli_setup

    if not cli_setup():
        auto_setup(__file__, logdir=None, devices=[
            "Android://127.0.0.1:5037/M960BDPT226TA?cap_method=JAVACAP&&ori_method=ADBORI",
        ])
else:
    import timeout_decorator

    # M960BDPT226TA WIFI 10.0.0.26 48003
    argv1 = str(sys.argv[1])
    argv2 = str(sys.argv[2])
    argv3 = str(sys.argv[3])
    argv4 = str(sys.argv[4])

    if argv2 == 'USB':
        conn_info = 'Android://127.0.0.1:5037/{uuid}?cap_method=JAVACAP&&ori_method=ADBORI'.format(
            uuid=argv1)
        device = connect_device(conn_info)
    else:
        # WIFI模式
        conn_info = 'Android://127.0.0.1:5037/{clientIp}:{port}'.format(
            clientIp=argv3,
            port=argv4)
        print(conn_info)
        device = connect_device(conn_info)

    poco = AndroidUiautomationPoco(device, use_airtest_input=True, screenshot_each_action=False)

    logdir = "{path}/static/source_img/{fileName}/{devices}".format(
        path=os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
        fileName=str(os.path.abspath(__file__).split('/')[-1]).split('.py')[0],
        devices=argv1)
    if not os.path.exists(logdir):
        os.makedirs(logdir)
    auto_setup(__file__,
               logdir=logdir,
               devices=[conn_info, ],
               project_root="{root}".format(root=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))


def start():
    if not dev:
        set_logdir(logdir)
        # set_current(str(sys.argv[1]))
    # 退到桌面
    home()
    stop_app('com.ss.android.ugc.livelite')
    sleep(1)
    start_app('com.ss.android.ugc.livelite')
    start_home()


def start_home():
    sleep(15)
    qf = poco(type="android.widget.TextView", name="com.ss.android.ugc.livelite:id/qf")
    if qf.exists():
        qf.click()
    uy = poco(type="android.support.v7.widget.RecyclerView", name="com.ss.android.ugc.livelite:id/uy")
    # uy.wait_for_appearance()
    if uy.exists():
        uy.swipe([0, 0.8], duration=0.25)
        sleep(2)
        uy.child("android.widget.RelativeLayout")[0].click()
    while True:
        loop_body()


@timeout_decorator.timeout(60)
def loop_body():
    print('loop_body......')
    a2 = poco(type="android.support.v4.view.ViewPager", name="com.ss.android.ugc.livelite:id/a2")
    ru = poco(type="android.widget.ProgressBar", name="com.ss.android.ugc.livelite:id/ru")
    a0r = poco(type="android.widget.TextView", name="com.ss.android.ugc.livelite:id/a0r")
    if ru.exists():
        sleep(random.randint(12, 16))
        a2.swipe([0, -0.9], duration=0.25)
    else:
        if a0r.exists():
            sleep(random.randint(12, 16))
            a0r.click()
            sleep(1)
            a2.swipe([0, -0.9], duration=0.25)
        else:
            a2.swipe([0, -0.9], duration=0.25)


def main():
    try:
        start()
    except Exception as e:
        print(e)


if __name__ == '__main__':
    main()
